package com.tanhua.listener;

import com.tanhua.domain.mongo.VideoScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(topic = "tanhua-video",consumerGroup = "tanhua-video-group")
public class VideoMQListener implements RocketMQListener<Map> {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void onMessage(Map message) {
        VideoScore videoScore = new VideoScore();
        videoScore.setUserId(Long.parseLong(message.get("userId").toString()));
        videoScore.setVideoId(Long.parseLong(message.get("vid").toString()));
        videoScore.setDate(System.currentTimeMillis());
        Integer type = (Integer) message.get("type");
        switch (type){
            case 1:
                videoScore.setScore(20D);
                break;
            case 2:
                videoScore.setScore(5D);
                break;
            case 3:
                videoScore.setScore(-5D);
                break;
            case 4:
                videoScore.setScore(10D);
                break;
            default:
        }
        mongoTemplate.insert(videoScore);
    }
}
